// pages/sign/card/exchange/exchange.js
var api = require("../../../../utils/api.js");
var util = require("../../../../utils/util.js");

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    cardInfo: [],
    exchangeCardInfo: false,
    exchangeSuccessView: false,
  },

  intercept: function(e) {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.updateCardInfo();
  },
  //更新兑换组信息
  updateCardInfo: function() {
    var card1 = {
      type: 1,
      number: 100000000,
      count: 0
    }
    app.globalData.cardInfo.member_card.first.forEach(card => {
      card1.number = Math.min(card1.number, card.number);
    });
    var card2 = {
      type: 2,
      number: 100000000,
      count: 0
    }
    app.globalData.cardInfo.member_card.end.forEach(card => {
      card2.number = Math.min(card2.number, card.number);
    });
    this.setData({
      cardInfo: [card1, card2],
      info: app.globalData.cardInfo,
    });
    // console.log(this.data.cardInfo);
  },


  change: function(e) {
    // console.log(e);
    var currentCount = e.detail;
    var currentCard = e.currentTarget.dataset.card;
    var exchangeBool = false;
    this.data.cardInfo.forEach(card => {
      if (card.type == currentCard.type) {
        card.count = currentCount;
      }
      if (card.count > 0) {
        exchangeBool = true;
      }
    });

    this.setData({
      exchangeCardInfo: exchangeBool,
      cardInfo: this.data.cardInfo,
    });
  },
  //兑换
  exchangeCard: function(e) {
    wx.showLoading({});
    api.exchangeCard(this.data.cardInfo[0].count + "," + this.data.cardInfo[1].count, () => {
      wx.hideLoading();
      this.setData({
        exchangeSuccessView: true,
      });
    }, res => {
      wx.hideLoading();
      wx.showToast({
        title: res,
        icon: 'none'
      })
    });
  },
  //兑换成功返回
  exchangeSuccess: function(e) {
    this.setData({
      exchangeSuccessView: false,
    });
    wx.navigateBack({})
  },
  //隐藏兑换界面
  hideExchangeView: function(e) {
    this.data.cardInfo.forEach(card => {
      if (card.type == 1) {
        app.globalData.cardInfo.member_card.first.forEach(card1 => {
          card1.number = card1.number - card.count;
        });
      } else {
        app.globalData.cardInfo.member_card.end.forEach(card1 => {
          card1.number = card1.number - card.count;
        });
      }
    });
    app.globalData.cardInfo.first_count += this.data.cardInfo[0].count;
    app.globalData.cardInfo.end_count += this.data.cardInfo[1].count;
    app.globalData.userStatus.point = parseFloat(app.globalData.userStatus.point) + this.data.cardInfo[0].count * parseFloat(this.data.info.card_1) + this.data.cardInfo[1].count * parseFloat(this.data.info.card_2);
    this.updateCardInfo();
    this.setData({
      exchangeSuccessView: false,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareSeekHelp();
  }
})